//
//  PersonInfoVIew.swift
//  maosheng
//
//  Created by lxl on 2019/8/15.
//

import UIKit

class PersonInfoVIew: PublicSwiftTableView {
    let headerView = PersonInfoHeaderView()
    let cellIdentifer = "MyCollectCell"
    let noImgIdentifer = "MyCollectNoImgCell"
    var userId : String?
    override init(frame: CGRect) {
        super.init(frame: frame)
        tableView.register(MyCollectCell.self, forCellReuseIdentifier: cellIdentifer)
        tableView.register(MyCollectNoImgCell.self, forCellReuseIdentifier: noImgIdentifer)
        setHeaderView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setHeaderView() -> Void {
        tableView.tableHeaderView = headerView
        headerView.snp.makeConstraints { (make) in
            make.top.left.right.equalTo(0)
            make.height.equalTo(222 + 170 * rateOfWidth)
            make.width.equalTo(SCREENW)
        }
        tableView.layoutIfNeeded()
        tableView.tableHeaderView = headerView
        refreshList = {[weak self] (isHeader) in
            self?.getUserBlog(isHeader: isHeader)
        }

    }
    
    
    func getUserBlog(isHeader : Bool) -> Void {
        let url =  "api/services/app/Blog/GetYourBlogPagingListAsync" + "?PageIndex=\(pageIndex)&PageSize=10&CreatorUserId=\(userId!)&Sorting=id desc".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
        
        SDataCenter.shared.SGetJsonDataWithUrl(str_url: url, params: nil) { [weak self] (responseData, isSuccess) in
            if self?.tableView.mj_header != nil{
                self?.tableView.mj_header.endRefreshing()
                self?.tableView.mj_footer.endRefreshing()
            }
            
            if isSuccess && responseData is [String : Any]{
                if isHeader{
                    self?.dataSource.removeAllObjects()
                }
                if let items = (responseData as! [String : Any])["items"]{
                    if let arr = items as? Array<Any>{
                        if let attentionArr = [CollectModel].deserialize(from: arr){
                            self?.dataSource.addObjects(from: attentionArr as [Any])
                        } 
                    }
                    
                }
                self?.checkNeedNoDataView()
                self?.setRefreshFooter()
            }else{
                if !isHeader{
                    self?.pageIndex = self!.pageIndex - 1
                }
                
            }
        }

    }
    
    
    
    
    
    
    
    func checkNeedNoDataView(){
       
//        makeConstraints({ (make) in
//            make.top.equalTo(0)
//            make.centerX.equalToSuperview()
//        })
        if self.dataSource.count == 0 {
            let str_msg : String =  "暂无创作"
            self.noDataView?.noDataImg.image = UIImage(named: "blog_empty")
            self.showTheNoDataView(title: str_msg)
        }else{
            if self.noDataView != nil{
                self.noDataView?.isHidden = true
            }
        }
        noDataView?.snp.updateConstraints({ (make) in
            make.top.equalTo(headerView.frame.size.height)
        })
    }
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        let model = self.dataSource[indexPath.row] as? CollectModel
        let images = model?.images
        var cell : MyCollectNoImgCell?
        if images != nil && images!.count > 0{
            cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifer) as! MyCollectCell
        }else{
            cell = tableView.dequeueReusableCell(withIdentifier: noImgIdentifer) as? MyCollectNoImgCell
        }
        let commentCount = model?.commentCount != nil ? model?.commentCount : "0"
        let time = model?.creationTime != nil ? model?.creationTime : ""
//        .replacingOccurrences(of: "T", with: " ")
        let creatTime = time?.components(separatedBy: "T").first
        let str_title = commentCount! + "条评论 " + creatTime!

        cell?.deleteBtn.setTitle(str_title, for: .normal)
        cell?.deleteBtn.tag = 401 + indexPath.row
        cell!.setCollectInfo(model: model!)
        cell?.selectionStyle = .none
        return cell!
    }
}
